package com.xzy.Controller;

import com.xzy.Service.StarsService;
import com.xzy.pojo.Stars;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Controller
public class StarsController {
    public final static String UPLOAD_DIR="H:\\springboot整合jpa\\整合JpaSwagger\\src\\main\\resources\\static";
    @Autowired
    StarsService starsService;
    @GetMapping("/stars/toStars")
    @ApiOperation("明星展示页面，需传入当前页数，页数小于0自动转为0，大于所有页数转为0")
    public String toStars(Model model,int page){
        System.out.println("明星展示页面1");
        int count= (int) starsService.count();
        int rows=count/3;
        if(count%3>0){
            rows++;
        }
        /*
        当前页小于0，返回首页，大于总页数返回首页
         */
        if (page<0){
            page=0;
        }else if (page>=rows){
            page=0;
        }
        try{
            Page<Stars> allStars = starsService.findAll(page);
            model.addAttribute("rows",rows);
            model.addAttribute("allStars",allStars);
            model.addAttribute("nowPage",page);
        }catch (Exception e){
            model.addAttribute("allStars",null);
        }finally {
            return "stars";
        }




    }
    
    @GetMapping("/stars/toUpdate")
    @ApiOperation("明星修改页面，需要传入id")
    public String toUpdate(Model model ,int id){
        System.out.println("跳转修改页面，id->"+id);
            Stars stars = starsService.findById(id).get();
            model.addAttribute("updateStars",stars);
            return "updateStars";
    }

    @PostMapping("/stars/update")
    @ApiOperation("明星修改,需传入明星类7属性,id,name,src前端不可改")
    public String update(Model model ,Stars stars){
        System.out.println(stars.toString());
       try{
           starsService.save(stars);
           model.addAttribute("updateStars",stars);
           model.addAttribute("updateStatus","修改成功");
       }catch (Exception e) {
           stars = starsService.findById(stars.getId()).get();
           model.addAttribute("updateStars", stars);
           model.addAttribute("updateStatus", "修改失败");
       }
        return "updateStars";
    }

    @GetMapping("/stars/toStars2")
    @ApiOperation("用来修改等操作返回首页，需当前页数，明星名")
    public String toStars(Model model,int page,String name){
        System.out.println("明星展示页面2");
        return this.toStars(model, page);
    }

    @RequestMapping(value = "/stars/toInsertStar",method = {RequestMethod.GET,RequestMethod.POST})
    @ApiOperation("跳转新增明星页面，无需参数")
    public String toInsertStar(){
        return "insertStar";
    }

    @PostMapping("/stars/insertStar")
    @ApiOperation("新增明星页面，需明星实体类")
    public String insertStar(Model model,Stars stars, MultipartFile pic) {
        System.out.println("新增明星页面");
        String src="/img/"+stars.getName()+".jpg";
        stars.setSrc(src);
        stars.setId(starsService.getMaxId()+1);

        try{
            synchronized (this){
                starsService.save(stars);
                pic.transferTo(new File(UPLOAD_DIR,src));
                model.addAttribute("insertStarMes","保存成功");
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
            model.addAttribute("insertStarMes","保存失败");
        }finally {
            return "insertStar";
        }
    }

    @ApiOperation("修改明星头像，需图片，明星名，当前页数作为参数")
    @PostMapping("/upload")
    public String upload(MultipartFile pic,String name,int nowPage,Model model) throws IOException {
        name="/img/"+name+".jpg";
        String picName=pic.getOriginalFilename();
        String ext=picName.substring(picName.lastIndexOf("."));
        if(ext.equals(".jpg")||ext.equals(".png")){
            try{
                synchronized (this){
                    pic.transferTo(new File(UPLOAD_DIR,name));
                    model.addAttribute("updateSrc","修改成功");
                }
            }catch (Exception e){
                model.addAttribute("updateSrc","修改失败");
            }
        }else {
            model.addAttribute("updateSrc","修改失败,仅支持png,jpg格式图片");
        }
            return this.toStars(model,nowPage);

    }

    @ApiOperation("删除明星,需要提供id")
    @GetMapping ("/stars/delete")
    public String delete(Model model,Integer id,int nowPage){
        try{
            starsService.deleteById(id);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return this.toStars(model,nowPage);
    }
}
